package aes.gui;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import aes.general.AdminControl;

/**
 * The Class AdminStudentStatsPanel. This panel provides statistical information
 * about spesific student for the admin's reports.
 */
public class AdminStudentStatsPanel extends JPanel{
	
	
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JButton btnPrev;
    private JLabel title;
	private JSeparator separator;
	private JTextField avgtext;
	private JTextField medtext;
	private JTextField numtext;
	private JComboBox sidBox;
	private ArrayList<Double> stats;
	private Vector<Vector<String>> studentsvec ;
	private JTextField stdtext;
	private DefaultTableModel model ;
	private JTable table;
	private JLabel lblStudenId;
	private ArrayList<Integer > grades;
	
	/**
	 * Instantiates a new admin student stats panel.
	 * 
	 * @param contentPane
	 *            the content pane
	 */
	public AdminStudentStatsPanel(JPanel contentPane) {
		this.jp = contentPane;
		initialize();
	}
	
	/**
	 * Initialize.
	 */
	private void initialize() {
		
		setLayout(null);
		title = new JLabel("Student Statistics");
		title.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		title.setBounds(252, 11, 136, 29);
		add(title);
		
		separator = new JSeparator();
		separator.setBounds(10, 43, 620, 2);
		add(separator);
		
		
		btnPrev = new JButton("Back");
		btnPrev.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AdminControl.setUserInfo(null);
				CardLayout cl = (CardLayout) (jp.getLayout());
				cl.show(jp, "reports");
				
			}
		});
		btnPrev.setBounds(189, 374, 107, 23);
		add(btnPrev);
		
	
		
		JLabel lblStudents = new JLabel("Student Name");
		lblStudents.setBounds(238, 56, 107, 14);
		add(lblStudents);
		
		JLabel lblA = new JLabel("Average");
		lblA.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblA.setBounds(50, 140, 69, 23);
		add(lblA);
		
		avgtext = new JTextField();
		avgtext.setEditable(false);
		avgtext.setFont(new Font("Tahoma", Font.BOLD, 14));
		avgtext.setBounds(150, 141, 47, 20);
		add(avgtext);
		avgtext.setColumns(10);
		
		JLabel lblMedian = new JLabel("Median");
		lblMedian.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblMedian.setBounds(49, 187, 69, 23);
		add(lblMedian);
		
		medtext = new JTextField();
		medtext.setFont(new Font("Tahoma", Font.BOLD, 14));
		medtext.setEditable(false);
		medtext.setBounds(150, 190, 47, 20);
		add(medtext);
		medtext.setColumns(10);
		
		JLabel lblDistribution = new JLabel("Distribution");
		lblDistribution.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblDistribution.setBounds(49, 241, 86, 23);
		add(lblDistribution);
		
		JPanel Tablepanel = new JPanel();
		Tablepanel.setBounds(49, 275, 452, 76);
		add(Tablepanel);
		
		Tablepanel.setLayout(null);
		
		String col[] = {"1","2","3","4","5","6","7","8","9","10"};
		String data[][] = {{}};
		model = new DefaultTableModel(data,col);
		
		
		table = new JTable(model);
		table.setBackground(new Color(245, 255, 250));
		table.setFont(new Font("Tahoma", Font.PLAIN, 12));
		table.setBounds(10, 11, 432, 36);
		Tablepanel.add(table);
		int j = 0;
		model.insertRow(j,new Object[]{"0-9","10-19","20-29","30-39","40-49","50-59","60-69","70-79"
				,"80-89","90-100"});
		
		JLabel lblNumberOfExams = new JLabel("Number of exams : ");
		lblNumberOfExams.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblNumberOfExams.setBounds(270, 146, 162, 29);
		add(lblNumberOfExams);
		
		numtext = new JTextField();
		numtext.setFont(new Font("Tahoma", Font.BOLD, 14));
		numtext.setEditable(false);
		numtext.setBounds(442, 152, 32, 20);
		add(numtext);
		numtext.setColumns(10);
		
		while((studentsvec = AdminControl.getUserInfo())== null);
		sidBox = new JComboBox();
		sidBox.setBounds(49, 98, 60, 20);
		add(sidBox);
		sidBox.addItem("-----");
		for(int i =0 ;i<studentsvec.size();i++)
			sidBox.addItem(studentsvec.get(i).get(0));
		sidBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int index = sidBox.getSelectedIndex(); 
				if(sidBox.getSelectedIndex() == 0){
					stdtext.setText(null);
				avgtext.setText(null);
				medtext.setText(null);
				numtext.setText(null);
				model.insertRow(1,new Object[]{null,null,null,null,null,null,null,null,null,null});
				model.removeRow(2);
			}
				else {
					stdtext.setText(studentsvec.get(index-1).get(1)+ " " + studentsvec.get(index-1).get(2));
					AdminControl.getStudentStat((String) sidBox.getSelectedItem());
					while((grades=AdminControl.getGrades())==null);
					if(grades.get(0) == (200))
					{
						JOptionPane.showMessageDialog(null, "No Statistics For This Student", "Error",
								JOptionPane.ERROR_MESSAGE);
						AdminControl.setGrades(null);
					}
					else{
					stats = AdminControl.CulcStats(AdminControl.getGrades()); 
					avgtext.setText(stats.get(0).toString());
					medtext.setText(stats.get(1).toString());
					numtext.setText(stats.get(2).toString());
					int[] dis = AdminControl.getDistribution(AdminControl.getGrades());
					model.insertRow(1,new Object[]{dis[0],dis[1],dis[2],dis[3],dis[4],dis[5],dis[6],dis[7],dis[8],dis[9]});
					model.removeRow(2);
					AdminControl.setGrades(null);
					}
				}
			}
		});	
		
		
		
		
		stdtext = new JTextField();
		stdtext.setEditable(false);
		stdtext.setBounds(330, 53, 86, 20);
		add(stdtext);
		stdtext.setColumns(10);
		
		lblStudenId = new JLabel("Studen Id");
		lblStudenId.setBounds(50, 80, 69, 14);
		add(lblStudenId);
		
	}
}


